# -*- coding:utf-8 -*-
__author__ = 'weikai'
from request_epicc.log import Logger
import threading,datetime,traceback
from  request_epicc.interface.isRenewal import isRenewal2
from request_cic.login import logincic
from  request_cic.request_getdata import getData,getData_CPlateNo
from request_cicc.cicc_standard_flow.flow_main import flow_main
from request_cicc.interface.login import login
from insert_session import get_session
from request_cic.vhlPlatform import getCarInof
from session import getSession
from request_cic.getCode import get_citycode
from request_pingan.login import Login
from request_cic.updateCarInfo import update_car_info
from request_pingan.entrance import pingan_request
from gevent import monkey; monkey.patch_socket()
monkey.patch_all()
monkey.patch_ssl()
import gevent
import thread
import json
log=Logger()

#车架号查询信息之后查询保费
def getSource(dt):
   try:
    base64strdt=get_session(['4','5',"1"])
    if len(base64strdt)!=0:
        sessiondt=getSession(base64strdt)
        requestcic=sessiondt['4']
        loginRes=sessiondt['5']
        pingansession=sessiondt['1']
    else:
        requestcic=logincic()
        loginRes=login()
        l=Login()
        pingansession=l.login()
    #log.info('dt=%s' % dt)

    vinNo=dt.get('vinNo','')
    insureCarId=dt.get('insureCarId','')
    if vinNo=='' or insureCarId=='':
        return
    dt['endDate']=dt.get("preInsureEndDate","")

    if dt.get('identitCard','')=='':
        dt['identitCard']='320123199106102810'
    dt['insuranceType']=dt.get("insuranceType","")
    '''
    #如果为空 先走查询信息
    if dt.get('engineNo','')=='' or dt.get('firstRegister','')=='' or dt.get('plateNumber','')=='' or dt.get('vehicleBrand','')=='':
        cardict=getCarInof(requestcic,vinNo)
        cityCode=get_citycode(cardict['gCPlateNo'])
        dt['cityCode'] = cityCode
        cardict['cityCode'] = cityCode
        #更新数据
        update_car_info(cardict,dt['insureCarId'])
        dt['custName']=cardict['hCAppNme']
        dt['plateNumber']=cardict['gCPlateNo']
        dt['vehicleBrand']=cardict['gCNoticeType']
        dt['firstRegister']=cardict['CFstRegYm']
        dt['engineNo']=cardict['CEngNo']
        dt['cityCode'] = cardict['cityCode']
        dt['cardict']=cardict
    '''
    client=dt['client']

    log.info(u"程序开始运行%s" % datetime.datetime.now())
    try:
        threads = []
        th = threading.Thread(target=getData, args=(requestcic, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=flow_main, args=(loginRes, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=pingan_request, args=(pingansession, dt))
        th.start()
        threads.append(th)
        for th in threads:
            th.join()
        print "程序结束运行%s" % datetime.datetime.now()

    except Exception,e:
        log.error(e)
        log.error(traceback.format_exc())

   except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())

#根据车牌号进行查询保费信息
def plateNo_getSource(dt):
   try:
    requestcic=dt['4']
    loginRes=dt['5']
    pingansession=dt['1']
   # logpingan=Login()
    #pingansession=logpingan.login()

    cdt={}
    cdt['identitCard']=dt['identifyNumber']
    cdt['custName']=dt['insuredName']
    cdt['plateNumber']=dt['licenseNo']
    cdt['vinNo']=dt['vinNo']
    cdt['engineNo']=dt['engineNo']
    cdt['insureCarId']=dt['insureCarId']
    cdt['cityCode']=get_citycode(dt['licenseNo'])
    cdt['endDate']=dt['endDate']
    cdt['firstRegister']=dt['enrollDate']
    cdt['vehicleBrand']=dt['brandName']
    cdt['NSeatNum']=dt.get('NSeatNum',999)
    cdt['client']=dt['client']
    client=cdt['client']
    cdt['sessionId']=dt['sessionId']
    cdt['isPhone']=dt['isPhone']
    cdt['insuranceType']=dt['insuranceType']
    #cdt['insureTypeGroupId']=dt['insureTypeGroupId']

    log.info(u"程序开始运行%s" % datetime.datetime.now())
    try:
        #使用多线程调用
        threads = []
        th = threading.Thread(target=getData, args=(requestcic, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=flow_main, args=(loginRes, dt))
        th.start()
        threads.append(th)
        th = threading.Thread(target=pingan_request, args=(pingansession, dt))
        th.start()
        threads.append(th)
        for th in threads:
            th.join()
        print "程序结束运行%s" % datetime.datetime.now()
        # g2 = gevent.spawn(getData_CPlateNo, requestcic,cdt)
        # g3 = gevent.spawn(flow_main, loginRes,cdt)
        # g4 = gevent.spawn(pingan_request, pingansession,cdt)
        # gevent.joinall([g2,g3,g4])
        #pingan_request(pingansession,cdt)
        #getData_CPlateNo(requestcic,cdt)
    except Exception,e:
        log.error(e)
        log.error(traceback.format_exc())

   except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())
   log.info(u"程序结束运行%s" % datetime.datetime.now())

